package hot_100;

public class 反转链表II {
    public static void main(String[] args) {

    }

    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode ans = new ListNode();
        ListNode po = new ListNode();
        ans.next = head;
        po.next = head;
        for (int i = 1; i < left; i++) {
            po = po.next;
        }
        ListNode pre = null;
        ListNode cur = po.next;
        for (int i = left; i <= right; i++) {
            ListNode temp = cur;
            cur = cur.next;
            temp.next = pre;
            pre = temp;
        }
        po.next.next = cur;
        po.next = pre;
        return ans.next;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
